package org.elasticsearch;

import com.obs.services.model.AccessControlList;
import com.obs.services.model.Permission;
import com.obs.services.model.GroupGrantee;
/**
 * ACL Enum
 */
public enum AclType {
    /**
     * Specifies the owner is granted {@link Permission#PERMISSION_FULL_CONTROL}. No one else has access rights.
     * <p>
     * This is the default access control policy for any new buckets or objects.
     * </p>
     */
    Private("private", AccessControlList.REST_CANNED_PRIVATE),

    /**
     * Specifies the owner is granted {@link Permission#PERMISSION_FULL_CONTROL} and the
     * {@link GroupGrantee#ALL_USERS} group grantee is granted
     * {@link Permission#PERMISSION_READ} access.
     * <p>
     * If this policy is used on an object, it can be read from a browser without
     * authentication.
     * </p>
     */
    PublicRead("public-read", AccessControlList.REST_CANNED_PUBLIC_READ),

    /**
     * Specifies the owner is granted {@link Permission#PERMISSION_FULL_CONTROL} and the
     * {@link GroupGrantee#ALL_USERS} group grantee is granted
     * {@link Permission#PERMISSION_READ} and {@link Permission#} access.
     * <p>
     * This access policy is not recommended for general use.
     * </p>
     */
    PublicReadWrite("public-read-write", AccessControlList.REST_CANNED_PUBLIC_READ_WRITE),

    /**
     * Specifies the owner is granted {@link Permission#PERMISSION_FULL_CONTROL} and the
     * {@link GroupGrantee#AUTHENTICATED_USERS} group grantee is granted
     * {@link Permission#PERMISSION_READ} access.
     */
    AuthenticatedRead("authenticated-read", AccessControlList.REST_CANNED_AUTHENTICATED_READ),

    /**
     * Specifies the owner is granted {@link Permission#PERMISSION_FULL_CONTROL} and the
     * {@link GroupGrantee#LOG_DELIVERY} group grantee is granted
     * {@link Permission#PERMISSION_WRITE} access so that access logs can be delivered.
     * <p>
     * Use this access policy to enable Huawei OBS bucket logging for a bucket.
     * The destination bucket requires these permissions so that access logs can
     * be delivered.
     * </p>
     */
    LogDeliveryWrite("log-delivery-write", AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE),

    /**
     * Specifies the owner of the bucket, but not necessarily the same as the owner of the
     * object, is granted {@link Permission#PERMISSION_FULL_CONTROL}.
     * <p>
     * Use this access policy when uploading objects to another owner's bucket.
     * This access policy grants the bucket owner read access to the object,
     * but does not give read access for all users.
     * </p>
     */
    BucketOwnerRead("bucket-owner-read", AccessControlList.REST_CANNED_BUCKET_OWNER_READ),

    /**
     * Specifies the owner of the bucket, but not necessarily the same as the owner of the
     * object, is granted {@link Permission#PERMISSION_FULL_CONTROL}.
     * <p>
     * Use this access policy to upload objects to another owner's bucket.  This
     * access policy grants the bucket owner full access to the object, but does
     * not give full access to all users.
     * </p>
     */
    BucketOwnerFullControl("bucket-owner-full-control", AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL);

    /**
     * The Huawei OBS header value representing the canned acl
     */
    private final String cannedAcl;
    private final AccessControlList accessControl;

    private AclType(String acl, AccessControlList accessControl) {
        this.accessControl = accessControl;
        this.cannedAcl = acl;
    }

    public String toString() {
        return cannedAcl;
    }

    public static AccessControlList getAcLByCode(String cannedAcl) {
        for (AclType aclEnum : values()) {
            if (aclEnum.cannedAcl.equals(cannedAcl)) {
                return aclEnum.accessControl;
            }
        }
        return null;
    }

    public String getCannedAcl() {
        return cannedAcl;
    }
}
